package com.tanhua.server.service;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.ErrorResult;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsService {

    @Reference
    private UserApi userApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private MovementMQService movementMQService;

    public ResponseEntity findCommentList(String publishId, int page, int pagesize) {
        //查询动态id对应的评论信息
        PageResult pageResult = commentApi.findComments(page, pagesize, publishId);
        List<Comment> commentVOList = pageResult.getItems();
        List<CommentVO> voList = new ArrayList<>();
        for (Comment comment : commentVOList) {
            CommentVO vo = new CommentVO();
            BeanUtils.copyProperties(comment, vo);
            vo.setId(comment.getId().toHexString());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            vo.setHasLiked(0);
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            voList.add(vo);
        }
        pageResult.setItems(voList);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 发表评论
     * @param publishId
     * @param content
     * @return
     */
    public ResponseEntity saveComment(String publishId, String content) {
        User user = userApi.findById(UserHolder.getUser().getId().intValue());
        String phone = user.getMobile();

        Boolean freezingTime = redisTemplate.hasKey("FreezingTime" + phone);
        //如果还在冻结时间内，并且冻结范围是1，才禁止登录
        //如果冻结时间结束，则当前用户冻结范围也取消
        if (!freezingTime) {
            redisTemplate.delete("FreezingRange" + phone);
        } else {
            //如果冻结非空还有冻结为2的情况下，禁止评论
            String number = redisTemplate.opsForValue().get("FreezingRange" + phone);
            if (number != null && "2".equals(number)) {
                return ResponseEntity.status(500).body(ErrorResult.prohibitComments());
            }
        }

        //1. 封装评论数据：当前用户  对  动态publishId 发表了评论，内容是content
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setContent(content);
        comment.setUserId(UserHolder.getUser().getId());
        //   评论类型：1点赞，2评论，3喜欢
        comment.setCommentType(2);
        //   评论内容类型：1对动态评论， 2对视频评论， 3对评论评论
        comment.setPubType(1);

        //2. 保存评论
        commentApi.save(comment);

        //大数据
        movementMQService.sendCommentPublishMsg(publishId);
        return ResponseEntity.ok(null);
    }
}
